package sk.tonyb.library.file;

import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.log4j.Logger;

/**
 * Class, which contains method, which write text to file. <br>
 * TODO - ak cesta k suboru neexistuje, tak ju vytvori a samotny subor tiez !!!
 * 
 * @author Anton Balucha
 * @since 04.05.2011
 * @last_modified 07.10.2011
 */
public class FileWriter {

	/** Log4j logger. */
	public static Logger logger = Logger.getLogger(FileWriter.class);

	/** Name of file into which will be content written. */
	private String fileName = null;

	/** Method, which store entered content, which will be written to file. */
	private String fileContent = null;
	
	/** 
	 * Constructor. <br>
	 * 
	 * @param fileName - name of file, into which will be content written. If file exists, content will be appended.
	 * @param fileContent - content, which will be written to file with fileName.
	 */
	public FileWriter(String fileName, String fileContent) {

		if(fileName == null || fileName.trim().isEmpty()) {
			throw new NullPointerException("Entered variable fileName is null or empty.");
		}
		
		if(fileContent == null) {
			throw new NullPointerException("Entered variable content is null.");
		}
		
		this.fileName = fileName;
		this.fileContent = fileContent;
		this.writeContentToFile();
	}

	/** 
	 * Method, which return name of file. <br>
	 * 
	 * @return fileName - name of file. 
	 */
	public synchronized String getFileName() {
		return this.fileName;
	}
	
	/** 
	 * Method, which return content of file. <br> 
	 * 
	 * @return fileContent - content of file.
	 */
	public synchronized String getFileContent() {
		return this.fileContent;
	}
	
	/** Method, which write content to file. */
	private synchronized void writeContentToFile() {
		this.append(this.fileContent);
	}
	
	/** 
	 * Method, which append content to file. <br>
	 * 
	 * @param fileContent - content, which will be appended to file. 
	 */
	public synchronized void appendContentToFile(String fileContent) {
		this.append(fileContent);
	}
	
	/** Method, which append content to file. */
	private synchronized void append(String content) {
		
		FileOutputStream fileOutputStream = null;
		DataOutputStream dataOutputStream = null;
		OutputStreamWriter outputStreamWriter = null;
		BufferedWriter bufferedWriter = null;
		
		try {

			fileOutputStream = new FileOutputStream(this.fileName, true);
			dataOutputStream = new DataOutputStream(fileOutputStream);
			outputStreamWriter = new OutputStreamWriter(dataOutputStream);
			bufferedWriter = new BufferedWriter(outputStreamWriter);
			
			bufferedWriter.append(content);

			bufferedWriter.close();
			outputStreamWriter.close();
			dataOutputStream.close();
			fileOutputStream.close();
		}
		
		catch(FileNotFoundException  e) {
			logger.error("FileNotFoundException", e);
		}

		catch(IOException e) {
			logger.error("IOException", e);
		}
		
		catch (Exception e) {
			logger.error("Exception", e);
		}
	}
}
